package com.pulook.daikuan.controller;

import java.math.BigDecimal;
import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.pulook.daikuan.dao.CheliangxinxiMapper;
import com.pulook.daikuan.dao.DiaochaMapper;
import com.pulook.daikuan.dao.FangkuanMapper;
import com.pulook.daikuan.dao.HeyueMapper;
import com.pulook.daikuan.dao.HuankuanMapper;
import com.pulook.daikuan.dao.SessionUtil;
import com.pulook.daikuan.dao.ShenqingMapper;
import com.pulook.daikuan.dao.YonghuMapper;
import com.pulook.daikuan.model.Cheliangxinxi;
import com.pulook.daikuan.model.Diaocha;
import com.pulook.daikuan.model.Fangkuan;
import com.pulook.daikuan.model.Heyue;
import com.pulook.daikuan.model.Huankuan;
import com.pulook.daikuan.model.Shenqing;
import com.pulook.daikuan.model.Yonghu;

@Controller
public class Houtaiye {

	@Inject
	ShenqingMapper s;
	public Shenqing s1;
	@Inject
	CheliangxinxiMapper c;
	public Cheliangxinxi c1;
	@Inject
	DiaochaMapper d;
	public Diaocha d1;
	@Inject
	HeyueMapper h;
	public Heyue h1;
	@Inject
	FangkuanMapper f;
	public Fangkuan f1;
	@Inject
	HuankuanMapper hk;
	public Huankuan hk1;
	@Inject
	YonghuMapper y;
	public Yonghu y1;
	
	public static String zhuantiao = "";
	
	/***
	 * 后台主页
	 */
	@RequestMapping("/htzhuye")
	public String htzy(Model m){
		
		Shenqing sq = new Shenqing();
//		sq.setShenqingyonghu(shenqingyonghu);
		sq.setZhuangtai("未审核");
		int num = s.getByConditionForNum(sq);
		sq.setStart(0);
		sq.setEnd(num);
		List<Shenqing> desc = s.getShenqingByConditionDesc(sq);
		
		Shenqing sq1 = new Shenqing();
//		sq.setShenqingyonghu(shenqingyonghu);
		sq1.setZhuangtai("通过");
		int num1 = s.getByConditionForNum(sq1);
		sq1.setStart(0);
		sq1.setEnd(num1);
		List<Shenqing> desc1 = s.getShenqingByConditionDesc(sq1);
		
		Shenqing sq2 = new Shenqing();
//		sq.setShenqingyonghu(shenqingyonghu);
		sq2.setZhuangtai("不通过");
		int num2 = s.getByConditionForNum(sq2);
		sq2.setStart(0);
		sq2.setEnd(num2);
		List<Shenqing> desc2 = s.getShenqingByConditionDesc(sq2);
		
		m.addAttribute("desc", desc);
		m.addAttribute("desc1", desc1);
		m.addAttribute("desc2", desc2);
		
		return "htzhuye";
	}
	
	/***
	 * 修改审核贷款计划页
	 */
	@RequestMapping("/htckshenqingye")
	public String htcksqy(Model m, int id){
		
		System.out.println("ddddd"+ id);
		Shenqing sq = s.getShenqingById(id);
//		m.addAttribute("f", sq);
		System.out.println("ssss===="+sq);
		SessionUtil.setAttributeForSession("shenqingid", id);
		SessionUtil.setAttributeForSession("daikuanjine", String.valueOf(sq.getDaikuanjine()));
		SessionUtil.setAttributeForSession("kehuming", sq.getKehuming());
		m.addAttribute("f", sq);
		return "htckshenqingye";
	}
	
	/***
	 * 审核修改表单处理
	 */
	@RequestMapping("htsqshenhe")
	public String htsqsh(String zhuangtai, int id){
		
		Shenqing sq = new Shenqing();
		sq.setId(id);
		sq.setZhuangtai(zhuangtai);
		s.updateById(sq);
		
		if (zhuangtai.equals("不通过")) {
			zhuantiao = "htzhuye";
		}else{
			zhuantiao = "htzhuantiaoye";
		}
//		 System.out.println("--------:"+ zhuantiao);
		return "forward:" +zhuantiao;
	}
	
	/*** ------zhuantiao
	 * 转跳处理
	 */
	@RequestMapping("htzhuantiaoye")
	public String htztyr(){
		
		return "htzhuantiaoye";
	}
	
	/***
	 *  贷款计划 完善页面
	 */
	@RequestMapping("/htsqwanshanye")
	public String htsqwanshanye(Model m, int id){
		
		SessionUtil.setAttributeForSession("shenqingid", String.valueOf(id));
		Shenqing sq = s.getShenqingById(id); //获取主申请表内容
		
		SessionUtil.setAttributeForSession("daikuanjine", String.valueOf(sq.getDaikuanjine()));
		SessionUtil.setAttributeForSession("kehuming", sq.getKehuming());
		
		Cheliangxinxi cl = new Cheliangxinxi();
		cl.setShenqingid(id);
		int clnum = c.getByConditionForNum(cl);
		cl.setStart(0);
		cl.setEnd(clnum);
		List<Cheliangxinxi> clasc = c.getCheliangxinxiByConditionAsc(cl);
		
		Diaocha dc = new Diaocha();
		dc.setShenqingid(id);
		int dcnum = d.getByConditionForNum(dc);
		dc.setStart(0);
		dc.setEnd(dcnum);
		List<Diaocha> dcasc = d.getDiaochaByConditionAsc(dc);
		
		Heyue hy1 = new Heyue();
		hy1.setShenqingid(id);
		int hynum = h.getByConditionForNum(hy1);
		hy1.setStart(0);
		hy1.setEnd(hynum);
		List<Heyue> hyasc = h.getHeyueByConditionAsc(hy1);
		
		Fangkuan fk = new Fangkuan();
		fk.setShenqingid(id);
		int fknum = f.getByConditionForNum(fk);
		fk.setStart(0);
		fk.setEnd(fknum);
		List<Fangkuan> fkasc = f.getFangkuanByConditionAsc(fk);
		
		Huankuan hk2 = new Huankuan();
		hk2.setShenqingid(id);
		int hknum = hk.getByConditionForNum(hk2);
		hk2.setStart(0);
		hk2.setEnd(hknum);
		List<Huankuan> hkasc = hk.getHuankuanByConditionAsc(hk2);
		
		String abiaoqian = "下一步，车辆信息填写";
		String aurl = "htcheliangtianxie?";
		int panduanzhi = 1;
		
		
		if (clasc.size() == 0) {
			abiaoqian = "下一步，车辆信息填写";
			aurl = "htcheliangtianxie?";
			 panduanzhi = 1;
		}else if (dcasc.size() == 0) {
			abiaoqian = "下一步，调查信息填写";
			aurl = "htdiaochatianxie?";
			 panduanzhi = 2;
		}else if (hyasc.size() == 0) {
			abiaoqian = "下一步，合约签约信息填写";
			aurl = "htheyuetianxie?";
			 panduanzhi = 3;
		}else if (fkasc.size() == 0) {
			abiaoqian = "下一步，放款信息填写";
			aurl = "htfangkuangtianxie?";
			 panduanzhi = 4;
		}else if(hkasc.size() == 0){
			abiaoqian = "下一步，还款信息填写";
			aurl = "hthuankuantianxie?";
			 panduanzhi = 5;
		}else {
			abiaoqian = "恭喜已完成所有步骤！";
			aurl = "#";
			 panduanzhi = 6;
		}
		
		
		m.addAttribute("sq", sq);
		m.addAttribute("clasc", clasc);
		m.addAttribute("dcasc", dcasc);
		m.addAttribute("hyasc", hyasc);
		m.addAttribute("fkasc", fkasc);
		m.addAttribute("hkasc", hkasc);
		
		m.addAttribute("abiaoqian", abiaoqian);
		m.addAttribute("aurl", aurl);
		m.addAttribute("panduanzhi", panduanzhi);
		
		return "htsqwanshanye";
	}
	
	/***
	 * 车辆信息填写页面
	 */
	@RequestMapping("/htcheliangtianxie")
	public String htclxxtianxieye(){
		
//		c.insert(cl);
		return "htcheliangtianxie";
	}
	/***
	 * 车辆信息表单处理
	 */
	@RequestMapping("/htclbiaodanchuli")
	public String htclbdcl(Cheliangxinxi cl){
		
		c.insert(cl);
		return "forward:htzhuantiaoye";
	}
	
	/***
	 * 调查信息填写页面
	 */
	@RequestMapping("/htdiaochatianxie")
	public String htdcxxtx(){
		return "htdiaochatianxie";
	}
	
	/***
	 * 调查信息表单处理
	 */
	@RequestMapping("/htdcbiaodan")
	public String htdcbd(Diaocha dc){
		
		d.insert(dc);
		return "forward:htzhuantiaoye";
	}
	
	/***
	 *  合约信息页
	 */
	@RequestMapping("/htheyuetianxie")
	public String hthytx(){
		
		return "htheyuetianxie";
	}
	/***
	 * 合约表单处理
	 */
	@RequestMapping("/hthybiaodan")
	public String hthybd(Heyue hy){
		
		h.insert(hy);
		return "forward:htzhuantiaoye";
	}
	
	/***
	 * 放款表单页
	 * @param fk
	 * @return
	 */
	@RequestMapping("/htfangkuangtianxie")
	public String htfktx(){
		
		
		return "htfangkuangtianxie";
	}
	
	/***
	 * 放款表单处理
	 */
	@RequestMapping("/htfkbiaodan")
	public String htfkbd(Fangkuan fk){
		
		f.insert(fk);
		return "forward:htzhuantiaoye";
	}
	
	/***
	 * 还款表单页
	 */
	@RequestMapping("/hthuankuantianxie")
	public String hthktx(Model m){
		
		String shenqingid = (String)SessionUtil.getAttributeFromSessionByKey("shenqingid");
//		int shenqingid = (int)obj;
		
		int b =  Integer.valueOf(shenqingid).intValue();
		System.out.println("id---:"+ b);
		Heyue hy = new Heyue();
		hy.setShenqingid(b);
		int num = h.getByConditionForNum(hy);
		hy.setStart(0);
		hy.setEnd(num);
		List<Heyue> asc = h.getHeyueByConditionAsc(hy);
		
//		int meiqihuankuan = asc.get(0).getDaikuanjine() ;
		
		BigDecimal lilv0 = new BigDecimal(Integer.toString(asc.get(0).getLilv()));
		BigDecimal baifen = new BigDecimal("100");
		BigDecimal lilv = lilv0.divide(baifen); // 计算利率
		
		BigDecimal zongjine = new BigDecimal(Integer.toString(asc.get(0).getDaikuanjine()));
		
		BigDecimal zonglixi = lilv.multiply(zongjine); // 获取总利息
		
		BigDecimal zonge = zongjine.add(zonglixi); // 计算利息加本金
		
		BigDecimal fenqi = new BigDecimal(Integer.toString(asc.get(0).getFenqishu()));
		
//		System.out.println("zonglixi===="+ zonglixi);
//		System.out.println("zonge===="+ zonge);
//		System.out.println("femqishu===="+ fenqi);
//		BigDecimal meiqihuankuan = zonge.divide(fenqi);
		int meiqihuankuan = zonge.intValue() / asc.get(0).getFenqishu();
		m.addAttribute("meiqihuankuan", meiqihuankuan);
		m.addAttribute("zongjine", asc.get(0).getDaikuanjine());
		m.addAttribute("asc", asc);
		return "hthuankuantianxie";
	}
	
	/***
	 * 还款表单处理
	 */
	@RequestMapping("/hthkbiaodan")
	public String hthkbd(Huankuan hk1){
		
		hk.insert(hk1);
		return "forward:htzhuantiaoye";
	}
	
	/***
	 * 已通过审核 申请贷款页
	 */
	@RequestMapping("/htsqtongguoye")
	public String htsqtgy(Model m){
		
		Shenqing sq = new Shenqing();
//		sq.setShenqingyonghu(shenqingyonghu);
		sq.setZhuangtai("通过");
		int num = s.getByConditionForNum(sq);
		sq.setStart(0);
		sq.setEnd(num);
		List<Shenqing> desc = s.getShenqingByConditionDesc(sq);
		m.addAttribute("desc", desc);
		
		return "htsqtongguoye";
	}
	
	/***
	 * 所有还款页一览
	 */
	@RequestMapping("hthuankuanyilanye")
	public String hthkyl(Model m){
		
		Huankuan hk1 = new Huankuan();
		hk1.setZhuangtai("进行中");
		int num = hk.getByConditionForNum(hk1);
		hk1.setStart(0);
		hk1.setEnd(num);
		List<Huankuan> desc = hk.getHuankuanByConditionDesc(hk1);
		
		Huankuan hk11 = new Huankuan();
		hk11.setZhuangtai("完结");
		int num1 = hk.getByConditionForNum(hk11);
		hk11.setStart(0);
		hk11.setEnd(num1);
		List<Huankuan> desc1 = hk.getHuankuanByConditionDesc(hk11);
		
		m.addAttribute("desc", desc);
		m.addAttribute("desc1", desc1);
		
		return "hthuankuanyilanye";
	}
	
	/***
	 * 还款详细表单处理
	 */
	@RequestMapping("/htxiangxibiaodanchuli")
	public String htxxbdcl(int id){
		
		Huankuan hk2 = hk.getHuankuanById(id);
		
		Huankuan hk1 = new Huankuan();
		hk1.setId(id);
		if (hk2.getShengyuqishu() == 1) {
			hk1.setShengyuqishu(hk2.getShengyuqishu() - 1);
			hk1.setZhuangtai("完结");
		}else if(hk2.getShengyuqishu() >= 2){
			hk1.setShengyuqishu(hk2.getShengyuqishu() - 1);
		}
		
		hk.updateById(hk1);
		return "forward:hthuankuanyilanye";
	}
	
	/***
	 * 贷款计划申请
	 */
	@RequestMapping("/htdaikuanshenqingye")
	public String htdkjhsqy(){
		
		return "htdaikuanshenqingye";
	}
	
	/***
	 * 贷款计划申请表单处理
	 */
	@RequestMapping("/htdaikuanjihuabiaodanchuli")
	public String htdkjhbdclssss(Shenqing sq){
//		System.out.println("z执行");
		sq.setZhuangtai("未审核");
		s.insert(sq);
		return "forward:htzhuye";
	}
	
	/**
	 *  用户查看一览
	 */
	@RequestMapping("/htyonghuye")
	public String hryhy(Model m){
		
		Yonghu yh = new Yonghu();
		yh.setQuanxian("员工");
		int num =y.getByConditionForNum(yh);
		yh.setStart(0);
		yh.setEnd(num);
		List<Yonghu> desc = y.getYonghuByConditionDesc(yh);
		
		Yonghu yh1 = new Yonghu();
		yh1.setQuanxian("用户");
		int num1 =y.getByConditionForNum(yh1);
		yh1.setStart(0);
		yh1.setEnd(num1);
		List<Yonghu> desc1 = y.getYonghuByConditionDesc(yh1);
		
		m.addAttribute("desc", desc);
		m.addAttribute("desc1", desc1);
		
		return "htyonghuye";
	}
}
